package org.fh;

import org.fh.domain.DataFrame;
import org.fh.domain.Word;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体发送消息类
 * @Author: wwy
 * @Date: 2020/10/20 14:45
 */
public class SendData {
    public static ApplicationContext applicationContext;
    private final static int head = 0xaa55;
    private int srcId;
    public int number;
    {
        srcId = Integer.parseInt(applicationContext.getEnvironment().getProperty("srcId"));
    }

    /**
     * 发送消息格式转换
     * @param cmd
     * @param destID
     * @param pData
     * @param length
     * @return
     */
    public DataFrame sendData(int cmd, int destID, List<String> pData, int length){
        DataFrame dataFrame = new DataFrame();
        dataFrame.setHead(intConvert(head,false));
        dataFrame.setCmd(intConvert(cmd,true));
        dataFrame.setSrcID(intConvert(srcId,true));
        dataFrame.setDestID(intConvert(destID,true));
        dataFrame.setLength(intConvert(length+14,true));
        dataFrame.setNumber(intConvert(number,true));
        if(pData == null){
            pData = new ArrayList<>();
        }
        dataFrame.setData(pData);
        return dataFrame;
    }

    public String dataFrameConvert(DataFrame dataFrame){
        String head = dataFrame.getHead().highLow;
        String length = dataFrame.getLength().lowHigh;
        String cmd = dataFrame.getCmd().lowHigh;
        String srcId = dataFrame.getSrcID().lowHigh;
        String destId = dataFrame.getDestID().lowHigh;
        String number = dataFrame.getNumber().lowHigh;
        List<String> data = dataFrame.getData();
        String dataArr = "";
        for (String datum : data) {
            dataArr = dataArr+datum;
        }
        String sendMsg = head+length+cmd+srcId+destId+number+dataArr;
        Word word = intConvert(makeChecksum(sendMsg), true);
        sendMsg = sendMsg+word.lowHigh;
        return sendMsg;
    }

    /**
     *
     * @param value 需要转换的数字
     * @param isLowHighConvert 是否高低位
     * @return
     */
    public Word intConvert(int value,boolean isLowHighConvert){
        String hexString = Integer.toHexString(value);
        int hexLength = hexString.length();
        if(hexLength<4){
            for(int i=0;i<4-hexLength;i++){
                hexString = "0" + hexString;
            }
        }

        if(isLowHighConvert){
            return new Word(hexString.substring(2,4),hexString.substring(0,2));
        }else{
            return new Word(hexString);
        }
    }


    public int makeChecksum(String data) {
        int hexSum = 0;
        if (data == null || data.equals("")) {
            return 0;
        }
        int length = data.length();
        if(length%2== 0){
            //偶数
            for(int i=0;i<data.length();i=i+2){
                int i1 = Integer.parseInt(data.substring(i, i + 2),16);
                hexSum +=i1;
            }
        }else{
            //奇数
            for(int i=0;i<data.length();i=i+2){
                int i1 = 0;
                if(i == length-1){
                    i1 = Integer.parseInt(data.substring(i, i + 1),16);
                }else{
                    i1 = Integer.parseInt(data.substring(i, i + 2),16);
                }

                hexSum +=i1;
            }
        }
        return hexSum;
    }



}
